'use strict';

import React, {Component, PropTypes} from 'react';
import {Link, browserHistory} from 'react-router';
import {Button} from 'amazeui-react';
import {Http} from '../lib';
import {API, STRING} from '../res';

export default class Header extends Component {
	/*
	 * 构造函数。
	 * @param props 组件属性。
	 */
	constructor(props) {
		super(props);
		this.state = {};
	}
	logout(){
		Http.post(API.logout,{},data=>{
			sessionStorage.clear()
			localStorage.clear()
			location.href = 'http://jydd.kidhub.cn/'
		})
		
	}
	/*
	 * 渲染。
	 */
	render() {
		let links = this.props.data.map((x, i) => {
			return (
				<Link
					key={i}
					to={x.href}
					style={{
						color: '#545454',
						display: x.title === undefined ? 'none' : 'inline',
					}}
					activeStyle={{
						color: '#ffffff',
						backgroundColor: '#0e90d2',
					}}
					className="am-padding"
					onClick={e => {
						this.props.onChange(i);
					}}
				>{x.title}</Link>
			);
		});

		return (
			<header style={{
				width: '100%',
				height: 120,
				lineHeight:"120px",
				padding: '0 30px',
				backgroundColor: '#ffffff',
				position: 'fixed',
				left: 0,
				top: 0,
			}}>
				<img 
					src={this.props.logo} 
				/>
				<div
					style={{
						position: 'absolute',
						top: 0,
						left: 0,
						display:'none'
					}}
				>
					<Button
						onClick={e => {
							Http.post(API.role, {
								role: 0,
							}, data => {
								if (data.errno === 0) {
									localStorage.setItem('userId', data.data.userid);
									localStorage.setItem('realname', data.data.realname);
									localStorage.setItem('areaId', data.data.areaId);
									localStorage.setItem('role', data.data.role);
									localStorage.setItem('unitId', data.data.unitId);
									browserHistory.push('/DDHome');
									window.location.href = window.location.href;
								} else {
									alert(data.errmsg);
								}
							});
						}}
					>督导室</Button>
					<Button
						onClick={e => {
							Http.post(API.role, {
								role: 1,
							}, data => {
								if (data.errno === 0) {
									localStorage.setItem('userId', data.data.userid);
									localStorage.setItem('realname', data.data.realname);
									localStorage.setItem('areaId', data.data.areaId);
									localStorage.setItem('role', data.data.role);
									localStorage.setItem('unitId', data.data.unitId);
									browserHistory.push('/DDHome');
									window.location.href = window.location.href;
								} else {
									alert(data.errmsg);
								}
							});
						}}
					>联络员</Button>
					<Button
						onClick={e => {
							Http.post(API.role, {
								role: 2,
							}, data => {
								if (data.errno === 0) {
									localStorage.setItem('userId', data.data.userid);
									localStorage.setItem('realname', data.data.realname);
									localStorage.setItem('areaId', data.data.areaId);
									localStorage.setItem('role', data.data.role);
									localStorage.setItem('unitId', data.data.unitId);
									browserHistory.push('/DDHome');
									window.location.href = window.location.href;
								} else {
									alert(data.errmsg);
								}
							});
						}}
					>组长</Button>
					<Button
						onClick={e => {
							Http.post(API.role, {
								role: 3,
							}, data => {
								if (data.errno === 0) {
									localStorage.setItem('userId', data.data.userid);
									localStorage.setItem('realname', data.data.realname);
									localStorage.setItem('areaId', data.data.areaId);
									localStorage.setItem('role', data.data.role);
									localStorage.setItem('unitId', data.data.unitId);
									browserHistory.push('/DDHome');
									window.location.href = window.location.href;
								} else {
									alert(data.errmsg);
								}
							});
						}}
					>督学</Button>
					<Button
						onClick={e => {
							Http.post(API.role, {
								role: 4,
							}, data => {
								if (data.errno === 0) {
									localStorage.setItem('userId', data.data.userid);
									localStorage.setItem('realname', data.data.realname);
									localStorage.setItem('areaId', data.data.areaId);
									localStorage.setItem('role', data.data.role);
									localStorage.setItem('unitId', data.data.unitId);
									browserHistory.push('/DDHome');
									window.location.href = window.location.href;
								} else {
									alert(data.errmsg);
								}
							});
						}}
					>镇教办</Button>
					<Button
						onClick={e => {
							Http.post(API.role, {
								role: 5,
							}, data => {
								if (data.errno === 0) {
									localStorage.setItem('userId', data.data.userid);
									localStorage.setItem('realname', data.data.realname);
									localStorage.setItem('areaId', data.data.areaId);
									localStorage.setItem('role', data.data.role);
									localStorage.setItem('unitId', data.data.unitId);
									browserHistory.push('/DDHome');
									window.location.href = window.location.href;
								} else {
									alert(data.errmsg);
								}
							});
						}}
					>学校</Button>
					<Button
						onClick={e => {
							Http.post(API.role, {
								role: 6,
							}, data => {
								if (data.errno === 0) {
									localStorage.setItem('userId', data.data.userid);
									localStorage.setItem('realname', data.data.realname);
									localStorage.setItem('areaId', data.data.areaId);
									localStorage.setItem('role', data.data.role);
									localStorage.setItem('unitId', data.data.unitId);
									browserHistory.push('/User');
									window.location.href = window.location.href;
								} else {
									alert(data.errmsg);
								}
							});
						}}
					>督导室管理员</Button>
					<Button
						onClick={e => {
							Http.post(API.role, {
								role: 7,
							}, data => {
								if (data.errno === 0) {
									localStorage.setItem('userId', data.data.userid);
									localStorage.setItem('realname', data.data.realname);
									localStorage.setItem('areaId', data.data.areaId);
									localStorage.setItem('role', data.data.role);
									localStorage.setItem('unitId', data.data.unitId);
									browserHistory.push('/User');
									window.location.href = window.location.href;
								} else {
									alert(data.errmsg);
								}
							});
						}}
					>镇教办管理员</Button>
					<Button
						onClick={e => {
							Http.post(API.role, {
								role: 8,
							}, data => {
								if (data.errno === 0) {
									localStorage.setItem('userId', data.data.userid);
									localStorage.setItem('realname', data.data.realname);
									localStorage.setItem('areaId', data.data.areaId);
									localStorage.setItem('role', data.data.role);
									localStorage.setItem('unitId', data.data.unitId);
									browserHistory.push('/User');
									window.location.href = window.location.href;
								} else {
									alert(data.errmsg);
								}
							});
						}}
					>学校管理员</Button>
				</div>
				<div className="am-inline-block am-fr">
					{links}
					<a 
						className="am-margin-left-xl"
						onClick={()=>{this.logout()}}
					>{STRING.logout}</a>
				</div>
			</header>
		);
	}
}

Header.propTypes = {
	data: PropTypes.array,
	title: PropTypes.string,
	logo: PropTypes.string,
	height: PropTypes.number,
	onChange: PropTypes.func,
};
Header.defaultProps = {
	data: [],
	height: 50,
	onChange: index => {},
};
